package com.quktzhuanka.app.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.quktzhuanka.app.R;
import com.quktzhuanka.app.adapter.MyMarketAdapter;
import com.quktzhuanka.app.base.BaseActivity;
import com.quktzhuanka.app.bean.Response;
import com.quktzhuanka.app.bean.TeamListBean;
import com.quktzhuanka.app.config.Constants;
import com.quktzhuanka.app.https.HttpUtils;
import com.quktzhuanka.app.https.onOKJsonHttpResponseHandler;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

/**
 * @Params:我的市场
 * @开发者:田哥哥
 * @日期:2018/10/25 15:38
 **/
public class MyMarketActivity extends BaseActivity {


    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.num_tv)
    TextView numTv;
    @BindView(R.id.yaoqingren_tv)
    TextView yaoqingrenTv;
    @BindView(R.id.one_person_tv)
    TextView onePersonTv;
    @BindView(R.id.two_person_tv)
    TextView twoPersonTv;
    @BindView(R.id.mListView)
    ListView mListView;
//    @BindView(R.id.bg_head)
//    LinearLayout bg_head;

    private List<TeamListBean.Teamlist> teamlists = new ArrayList<>();
    private MyMarketAdapter myMarketAdapter;

    private int select = 1;

    @Override
    protected void initUI() {
        setContentView(R.layout.activity_my_market);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    protected void initData() {
//        tvLeft.setVisibility( View.VISIBLE);
        tvTitle.setText("我的市场");
        tvTitle.setTextColor(getResources().getColor(R.color.white));
//        Drawable drawable1 = getResources().getDrawable(R.mipmap.icon_back_while);
//        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
//        tvLeft.setCompoundDrawables(drawable1, null, null, null);
        myMarketAdapter = new MyMarketAdapter(getComeActivity(), R.layout.my_marke_item, teamlists);
        mListView.setAdapter(myMarketAdapter);
//        bg_head.setBackground(getResources().getDrawable(R.drawable.bg_user_top));
        selectView(0);
    }

    @Override
    protected void initListener() {

    }


    /**
     * @Params:获取用户团队信息
     * @开发者:田哥哥
     * @日期:2018/10/25 15:59
     **/
    private void getTeamList() {
        RequestParams requestParams = new RequestParams();
        HttpUtils.post( Constants.GET_GETTEAMLIST_NEW, requestParams, new onOKJsonHttpResponseHandler<TeamListBean>( new TypeToken<Response<TeamListBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(throwable.getMessage());
            }

            @Override
            public void onSuccess(int statusCode, Response<TeamListBean> datas) {
                if (datas.isSuccess()) {
                    TeamListBean datasData = datas.getData();
                    if (datasData != null) {
                        numTv.setText(datasData.getReferrer_num());
                        yaoqingrenTv.setText("我的邀请人：" + datasData.getEferrer_name());
                        onePersonTv.setText(String.format("第一人市场\n%s人", datasData.getTeam1_num()));
                        twoPersonTv.setText(String.format("第二人市场\n%s人", datasData.getTeam2_num()));
                        teamlists.clear();
                        if (select == 1) {
                            teamlists.addAll(datasData.getTeamlist1());
                        } else {
                            teamlists.addAll(datasData.getTeamlist2());
                        }

                    }
                } else {
                    showToast(datas.getMsg());
                }
                myMarketAdapter.notifyDataSetChanged();
            }

            @Override
            public void onStart() {
                super.onStart();
                showLoadingDialog();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeLoadingDialog();
            }
        });
    }


    @OnClick({R.id.tv_left, R.id.one_person_tv, R.id.two_person_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.one_person_tv:
                selectView(0);
                break;
            case R.id.two_person_tv:
                selectView(1);
                break;
        }
    }

    private void selectView(int position) {
        TextView[] textViews = {onePersonTv, twoPersonTv};
        for (TextView textView : textViews) {
            textView.setTextColor(getResources().getColor(R.color.white));
            textView.setBackground(getResources().getDrawable(R.drawable.team_user_right));
        }
        textViews[position].setTextColor(getResources().getColor(R.color.black));
        textViews[position].setBackground(getResources().getDrawable(R.drawable.team_user_left));

        //请求
        getTeamList();

        select = position + 1;
    }
}
